String.prototype.parseInt = function(){
	return parseInt(this);
}
document.addEventListener("beforeload", handleBeforeLoadEvent, true);

blockedFlash = [];

function loadContent(event)
{

    var element = event.target;

    element.removeEventListener("mouseup", loadContent, false);
    
    element.allowedToLoad = true;

    var nextSibling = element.nextSibling;
    var parentNode = element.parentNode;
    
    if(!parentNode){
        return;
    }

    element.className = element.className.replace(/blocked\-content/gi,"");

    while(element && element.parentNode && element.parentNode.nodeName === "OBJECT"){
        element = element.parentNode;
        element.removeEventListener("mouseup", loadContent, false);
    
        element.allowedToLoad = true;
    
        var nextSibling = element.nextSibling;
        var parentNode = element.parentNode;
    
        element.className = element.className.replace(/blocked\-content/gi,"");
    }
        
    parentNode.removeChild(element);
    parentNode.insertBefore(element, nextSibling);

	event.stopPropagation();
	event.preventDefault();
}

function handleBeforeLoadEvent(event) {
	var element = event.target;
    
    //Ignore SVGs, we like those
    if (element.type == "image/svg+xml")
        return;
    
	if (element.allowedToLoad)
		return;
	
	if(element.className.indexOf("blocked-content") != -1){
		event.preventDefault();
		return;
	}

	if (safari.self.tab.canLoad(event, { url: event.url, nodeName: element.nodeName, width: parseInt(document.defaultView.getComputedStyle(element,"").getPropertyValue("width")), height: parseInt(document.defaultView.getComputedStyle(element,"").getPropertyValue("height")), elid: element.id, className: element.className, windowURL: window.location.href, type: element.type })){
		return;
    }

	event.preventDefault();

	if(!element.id){
		element.id = "plugin-instance-"+Math.uuid(5,16);
	}
		
    if(element.src && element.src.toLowerCase().indexOf("youtube.com/embed/") != -1){
        element.outerHTML = element.outerHTML.replace(/iframe/gi,"embed").replace("/embed/","/v/");
	}else{	
        safari.self.tab.dispatchMessage("loadHTML5Video", element.id);
        blockedFlash.push(element);
        
        element.className += " blocked-content";
        
        if(element.type == "application/x-shockwave-flash"){
            element.type = "application/futuresplash";
        }
        
        element.addEventListener("mouseup", loadContent, false);
	}
}

function receiveSettings(){
	
}

function handleMessage(event){
	if(event.name == "unblockAll"){
		unblockAll();
	}
	if(event.name == "settings"){
		receiveSettings(event.message);
	}
}

safari.self.addEventListener("message", handleMessage, false);

function unblockAll() {
	for(i in blockedFlash){
		var event = document.createEvent("MouseEvents");
		event.initEvent("mouseup",true,true);
		blockedFlash[i].dispatchEvent(event);
	}
}

function handleContextMenu(event){
	var userInfo = {};
	var el;
	if(event.target.nodeName == "CANVAS" && event.target.videoEl){
	   el = event.target.videoEl;
	}else{
	   el = event.target;
	}
	userInfo.nodeName = el.nodeName;
	userInfo.src = el.src;
	userInfo.id = el.id;
	userInfo.className = el.className;
	userInfo.fmt = el.fmt;
	if(el.siteName){
	   userInfo.targetName = el.siteName;
	}
	if(el.siteURL){
	   userInfo.targetURL = el.siteURL;
	}
	safari.self.tab.setContextMenuEventUserInfo(event, userInfo);
}
document.addEventListener("contextmenu", handleContextMenu, false);